<template>
  <div class="searchinputcom">
    <form @submit.prevent="submitForm">
      <div class="search1">
        <select id="searchselect" v-model="searchselect">
          <option value="岗位">按岗位搜索</option>
          <option value="公司">按公司搜索</option>
        </select>
        <input type="text" v-model="searchinput" placeholder="请输入搜索内容" />
        <input type="submit" value="搜索" id="bt" />
      </div>
    </form>
  </div>
</template>
<script>
import axios from "axios";
export default {
  data() {
    return {
      searchselect: "岗位",
      searchinput: "",
      activeIndex: "", // 默认选中的路由
    };
  },
  created() {
    // 在组件创建时获取当前路由的名称，并设置为 activeIndex 的初始值
    this.activeIndex = this.$route.name;
  },
  methods: {
    submitForm() {
      let data = new FormData();
      data.append("choice", this.searchselect);
      data.append("keyword", this.searchinput);
      axios
        .post("http://localhost:8080/hqdx/seeker/searchforPosition", data)
        .then((response) => {
          if (response.data.success == true) {
            console.log("activeIndex",this.activeIndex);
            if (response.data.data.choice2 == "岗位") {
              sessionStorage.setItem(
                "searchValuePos",
                JSON.stringify(response.data.data.items)
              );
              //避免重复跳转
              if (this.activeIndex === "SearchPos") {
                this.$emit('updataPos');
                return;
              }
              //无重复跳转
              else {
                this.$router.push({
                  name: "SearchPos",
                });
              }
            } else {
              sessionStorage.setItem(
                "searchValueCom",
                JSON.stringify(response.data.data.items)
              );
              //避免重复跳转
              if (this.activeIndex === "SearchCom") {
                this.$emit('updataCom');
                
                return;
              }
              //无重复跳转
              else {
                this.$router.push({
                  name: "SearchCom",
                });
              }
            }
          } else {
            alert("无法找到搜索内容");
          }
        })
        .catch((error) => {
          console.log(error);
        });
    },
  },
};
</script>
<style scoped>
.searchinputcom {
  margin-top: 50px;
  margin-bottom: 50px;
}
.search1 {
  width: 600px;
  height: 45px;
  background-color: white;
  margin-top: 4%;
  margin-left: 32%;
  border-radius: 15px;
}
.search1 input {
  margin-left: 0px;
  border: none;
  outline: none;
  width: 410px;
  height: 40px;
  border-radius: 15px;
  font-size: large;
}
#searchselect {
  border: none;
  outline: none;
  width: 100px;
  height: 40px;
  border-radius: 15px;
}
#bt {
  background-color: white;
  width: 70px;
  border: none;
  outline: none;
  cursor: pointer;
  /* 将鼠标样式改为指针样式 */
  transition: all 0.3s ease;
  /* 添加过渡效果 */
}

#bt:hover {
  background-color: #aababa;
}
</style>
